// Commonly used javascript functions
// 
// Functions for Showing/Hiding, Expanding/Contracting
//    showMe(id) -- pass in an id and the object is displayed
//    hideMe(id) -- pass in an id and the object is hidden
//    showMoreLess(d, sectionId)
//
// Select tag functions
//    showOther(selectId, otherFieldId) -- if "other" is selected in the selectId, the otherFieldId is shown. 
//			Otherwise, hidden.
//	  showIfValueSelected(value, sectionId) -- if value == "none", then hide sectionId.  
//			Otherwise, show sectionId.
//    moveItems(sourceList, destList) -- selected items in sourceList are moved from sourceList to destList.
//
// Check box functions
//    showIfChecked(checked, sectionId) -- if checked is true, sectionId is displayed.  Otherwise, hidden.
//    toggleIfChecked(checked, checkedId, uncheckedId) -- if checked is true, checkedId is shown and uncheckedId is hidden.
//			If checked is false, checkedId is hidden and uncheckedId is shown.
//	   setUnknown(checked, textFieldId) -- if checked is true, then clear & disable the text field.
// 			Otherwise, restore the text field.
//
// Radio Buton functions
//	   clearRadioButtons(buttonGroup) -- sets all radio buttons in the group to unchecked.
//
// Table functions
//		deleteRow(table, row) -- deletes the row in from the table
//
// Pop Up Window functions
//	   popUpWindow(URLStr, left, top, width, height) 
//
// Other functions
//	   countChars(textStr, countDisplay) -- Counts the chracters in textStr and updates countDisplay.
//			textStr is any text string (usually the contents of a text area)
//			countDisplay is the id of a span, td, etc. The innerHTML will be updated with the new total
// 			Returns the length of the text string.



// ---------- Functions for Showing/Hiding, Expanding/Contracting ----------

// show by id
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function showMe(myID) {
	var element= document.getElementById(myID); 
	element.style.display='inline';
}

// hide by id
function hideMe(myID) {
	var element = document.getElementById(myID);
	var element2 = document.getElementById("lefttd");
    var element3 = document.getElementById("righttd");
	element.style.display='none';
    element2.style.width='0%';
    element3.style.width='100%';
}

function showIfValue(elem, testVal, targetId){
	if (elem.value==testVal) showMe(targetId)
	else hideMe(targetId);
}
// Used to expand on contact a show more or less section.
// Snippet: CCC/Layout/More Less Section
function showMoreLess(d, sectionId) {
	hideMe(sectionId + '_less');
	hideMe(sectionId + '_more');
	
	showMe(sectionId + d);
}



// ---------- Select Tag Functions ----------

// Gets value of selected option of selectId.  If value == Other, then otherFieldId is shown.  
// Else it's hidden.
// Snippet:  CCC/User Input/Text Field with Unknown
function showOther(selectId, otherFieldId) {
	var selectedIndex = document.getElementById(selectId).selectedIndex;
	var selectedOption = document.getElementById(selectId).options[selectedIndex].value;
	
	if (selectedOption == "Other") {
		showMe(otherFieldId);
	} else {
		hideMe(otherFieldId);
	}
}


// if the selectedValue != none, then show the section sectionId.
// if the selectedValue == none, then hide sectionId.
function showIfValueSelected(selectedValue, sectionId) {
	if (selectedValue == null) {
			// something is wrong -- don't do anything.
	} else if (selectedValue == 'none') {
			hideMe(sectionId);
	} else {
			showMe(sectionId);
	}
}


// Moves selected items from sourceList to destList.  Items are added at the bottom of destList.
function moveItems(sourceList, destList) {
	var src = document.getElementById(sourceList);
	var dest = document.getElementById(destList);
	
	for (var i = 0; i < src.length; i++) {
		if (src.options[i].selected) {
			var newOpt = document.createElement("option");
			newOpt.text = src.options[i].text;
			newOpt.value = src.options[i].value;
			dest.options.add(newOpt);
			src.options.remove(i);
		}
	}
}
// Moves selected items up or down the list.
// In order to move the selected items up set 'to' to +1, in order to move them down set 'to' to -1.
function moveUpDown(index,to) {
	var list = document.getElementById('selected');
	var total = list.options.length-1;
	if (index == -1) return false;
	if (to == +1 && index == total) return false;
	if (to == -1 && index == 0) return false;
	var items = new Array;
	var values = new Array;
	for (i = total; i >= 0; i--) {
		items[i] = list.options[i].text;
		values[i] = list.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
			list.options[i] = new Option(items[i + to], values[i]);
			i--;
		}else {
			list.options[i] = new Option(items[i], values[i]);
		}
	}
	list.focus();
}




// ---------- Checkbox Functions ----------

// if check is true, show the section.
// Otherwise, hide it.
function showIfChecked(checked, sectionId)  {
	if (checked)  {
		showMe(sectionId);
	} else {
		hideMe(sectionId);
	}
}

	
// if checked is true, then checkedId is shown and uncheckedId is hidden.
// Else, if unchecked, then checked Id is hidden and uncheckedId is shown.
// Both checkedId and uncheckedId can be null.
function toggleIfChecked(checked, checkedId, uncheckedId) {
	if (checked) {
		if (checkedId != null) 		showMe(checkedId);
		if (uncheckedId != null)	hideMe(uncheckedId);
	} else {
		if (checkedId != null) 		hideMe(checkedId);
		if (uncheckedId != null)	showMe(uncheckedId);
	}
}

	
	
// if checked is true, then clear & disable the text field.
// otherwise, restore the text field.
// Snippet: CCC/User Input/Text Field with Unknown
function setUnknown(checked, textFieldId) {
	var element = document.getElementById(textFieldId);
	
	if (checked) {
		// clear & disable the text field
		element.value = "";
		element.disabled = true;
		element.style.backgroundColor = "#f7f7f7";
	
	} else {
		// restore the text field
		element.disabled = false;
		element.style.backgroundColor = "#fff";
	}
}

//Checks or unchecks a group of checkboxes based on whether a select all checkbox is checked.
//Parameters:
//checkAll - the select all checkbox passed as 'this'
//formName - the name of the form with the checkbox group
//group - the name shared by all checkboxes in a group
function setAllCheckboxes(checkAll, formName, group){
	var form = eval("document." + formName);
	var checkboxes = eval("form." + group);
	for (i=0; i<checkboxes.length; i++){
		checkbox = checkboxes[i];
		if(checkAll.checked) checkbox.checked=true;
		else checkbox.checked=false;
	}	
}

// ---------- Radio Button Functions ----------

function clearRadioButtons(buttonGroup) { 
	for (i=0; i < buttonGroup.length; i++) { 
		if (buttonGroup[i].checked == true) { 
			buttonGroup[i].checked = false;
		}
	} 
} 



// ---------- Table Functions ----------

function deleteRow(tableId, row) {
	var i=row.rowIndex;
	document.getElementById(tableId).deleteRow(i);	
}


// ---------- Pop Up Window Functions ----------

var popUpWin=0;

function popUpWindow(URLStr, left, top, width, height) {
  if(popUpWin) {
    if(!popUpWin.closed) popUpWin.close();
  }

  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}


// ---------- General Functions ----------


// Counts the chracters in textStr and update countDisplay
//		textStr is any text string (usually the contents of a text area)
//		countDisplay is the id of a span, td, etc. The innerHTML will be updated with the new total
// Returns the length of the text string.

function countChars(textStr, countDisplay) {
	var count = textStr.length;
	document.getElementById(countDisplay).innerHTML = count;
	
	return count;
}

// browser detection script to use with CSS files
var css_browser_selector = function() {
var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();



//show-hide DIV function for Select element: <select name="" onchange="javascript:showdiv('ab')" id="ab">
function showdiv (selectId) {
	var selectedIndex = document.getElementById(selectId).selectedIndex;
	var selectedOption = document.getElementById(selectId).options[selectedIndex].value;
	
	if (selectedOption == 'p') {
		document.getElementById('option').style.display = 'block';
	}
	else {  document.getElementById('option').style.display = 'none'; } // 'option' is DIV id
}


function contractF() { document.getElementById("mainPanel2").className='contract'; }
function expandF() { document.getElementById("mainPanel2").className='expand'; }

function showHyperlinkUnderline(divId, currentPage){
	   var divElement = document.getElementById(divId);
	   if(divElement != undefined){
		   var allHyperlinks = divElement.getElementsByTagName("a");
		   var length = allHyperlinks.length;
		   var hyperlink, href;
		   
		   //first do whole url match(including parameters, but excluding &superId=X&resId=X)
		   //traverse from tail to head in order to find the leaf menu
		   for(var i=length-1; i>=0; i--){
			   hyperlink = allHyperlinks[i];
			   href = hyperlink.getAttribute("href");
			   if(href.lastIndexOf(currentPage) != -1){
				   hyperlink.style["textDecoration"]=" underline";
				   return;
		       }
	       }
		   
		   //if not found, do partial url match(excluding parameters)
		   var mainUrl = currentPage;
		   var questionMarkPos = mainUrl.indexOf("?");
		   if (questionMarkPos != -1) {
			   mainUrl = mainUrl.substring(0, questionMarkPos);
		   }
		   for(i=length-1; i>=0; i--){
			   hyperlink = allHyperlinks[i];
			   href = hyperlink.getAttribute("href");
			   if(href.lastIndexOf(mainUrl) != -1){
				   hyperlink.style["textDecoration"]=" underline";
				   return;
		       }
	       }
		   
    }  
}

function StringBuffer() {   
    this.__strings__ = new Array;   
}

StringBuffer.prototype.append = function (str) { 
this.__strings__.push(str);   
}

StringBuffer.prototype.toString = function() {   
return this.__strings__.join("");   
}

function trim(str) 
{ 
   return str.replace(/(^\s*)|(\s*$)/g, ""); 
} 